﻿using System;
using System.Collections.Generic;
using Best.SmartCms.Dao;
using Best.SmartCms.Domain;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Dto;
using Best.SmartCms.Service.BizException;
using Best.SmartCms.Service.Common;
using Best.SmartCms.Utility;

namespace Best.SmartCms.Service
{
    /// <summary>
    /// Role服务
    /// </summary>
    public class RoleService : IService
    {
        private readonly RoleDao _roleDao;
        private readonly WorkContext _workContext;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mySqlDbContext"></param>
        /// <param name="workContext"></param>
        public RoleService(MySqlDbContext mySqlDbContext, WorkContext workContext)
        {
            _workContext = workContext;
            _roleDao = new RoleDao(mySqlDbContext);
        }

        

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResponse<RoleListDto> PageList(RoleListQueryDto query)
        {
            var list = _roleDao.PageList(query, out var totalCount, out var totalPages);
            var pageList = TinyMapperExtensions.Map<List<Role>, List<RoleListDto>>(list);
            return new PagedResponse<RoleListDto>(pageList, query.PageIndex,query.PageSize,totalCount,totalPages);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        public void Create(RoleEditDto dto)
        {
            var entity = TinyMapperExtensions.Map<RoleEditDto, Role>(dto);
            entity.CreatedTime = DateTime.Now;
            entity.CreatorId = _workContext.UserId;
            entity.CreatorName = _workContext.UserName;
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _roleDao.Add(entity);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        public void Edit(RoleEditDto dto)
        {
            var entity = _roleDao.GetSingle(dto.Id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{dto.Id}的数据");
            }

            TinyMapperExtensions.Map(dto, entity);
           
            entity.UpdatorId = _workContext.UserId;
            entity.UpdatorName = _workContext.UserName;
            entity.UpdatedTime = DateTime.Now;
            _roleDao.Update(entity);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoleEditDto Info(long id)
        {
            var entity = _roleDao.GetSingle(id);
            return TinyMapperExtensions.Map<Role, RoleEditDto>(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            var entity = _roleDao.GetSingle(id);
            if (entity == null)
            {
                throw new ParameterNullException($"未找到{id}的数据");
            }

            _roleDao.Delete(entity);
        }
    }
}
